-- Emails are constructed dynamically, using an extensive list of pregenerated statements.
-- The server puts them together by indexes and supplies the variables to be replaced in the strings.
-- The client then uses these instructions to construct the email as a (hopefully) cohesive email.

EMAIL_PERSONAL = 0
EMAIL_INDEPENDANT = 1
EMAIL_CORPORATE = 2
EMAIL_SHADY = 3

EMAIL_GREETING = 0
EMAIL_BREAK = 1
EMAIL_BUSINESS = 2
EMAIL_PAYMENT = 3
EMAIL_CAUTION = 4
EMAIL_ATTACHMENT = 5
EMAIL_HOW_TO_CONTACT = 6
EMAIL_INCLUDE_FILE = 7
EMAIL_SIGNATURE = 8

EMAIL_MISSION_SABOTAGE_SINGLE = 9

-- Personal
EMAIL = {}
EMAIL[EMAIL_PERSONAL] = {}
EMAIL[EMAIL_PERSONAL][EMAIL_BREAK] = {"\n"}
EMAIL[EMAIL_PERSONAL][EMAIL_GREETING] = {
	"Hey &PLAYER&, it's me, &FNAME&, again. ",
	"&PLAYER& my man! How are you! It's me, &FNAME&! ",
	"&PLAYER& old pal! I hope you're doing okay. It's me, &FNAME&. ",
	"It's me, &FNAME&. How have you been, mate? It's been a while. "}
	
EMAIL[EMAIL_PERSONAL][EMAIL_BUSINESS] = {
	"You know, from &BUSINESS&?"}
	
EMAIL[EMAIL_PERSONAL][EMAIL_PAYMENT] = {
	"As to payment, I'd like to keep this business like. I hope &PAYMENT& credits is enough. ",
	"I'm not asking you to go out of your way and do this for free; How does &PAYMENT& credits sound to you? ",
	"I don't want to sound stiff, but I don't want to leech off you, man... Please, accept my offer of &PAYMENT& credits for this, if you decide to do it. ",
	"You know how I am about these kind of things; I don't believe in free rides. Would &PAYMENT& credits would be a sufficient amount for a job like this? What do you think? "}
	
EMAIL[EMAIL_PERSONAL][EMAIL_CAUTION] = {
	"Be careful, though, man. I've heard that &CAUTION&. Watch yourself. ",
	"I'm sure this isn't an issue for you, but I won't be able to live with myself if I don't warn you about the fact that &CAUTION&. ",
	"There is a rumor going on, though, that &CAUTION&. I don't know if its true or not, but I think it may not be a bad idea to think about it at least. "}
	
EMAIL[EMAIL_PERSONAL][EMAIL_ATTACHMENT] = {
	"And of course, what good would all this do you without some additional information. I've attached the &ATTACHMENT& to this document. ",
	"Oh! I almost forgot! Here is the &ATTACHMENT&. It may be kind of hard to do this without it! ",
	"The &ATTACHMENT& is attached to the end of the document. "}
	
EMAIL[EMAIL_PERSONAL][EMAIL_HOW_TO_CONTACT] = {
	"Once you're done, please give me a buzz at &EMAIL&. ",
	"As soon as it's convenient, do you think you can let me know when the job's done? You can contact me at &EMAIL&. ",
	"I don't mean to rush you, but this is kind of important. Do you think you can let me know when you're done? My email is &EMAIL. ",
	"When you're done, please send me a message at &EMAIL&. "}

EMAIL[EMAIL_PERSONAL][EMAIL_INCLUDE_FILE] = {
	"Just send the file as an attachment, if you can. ",
	"As to the file, you can simply attach it to your response. ",
	"I don't mean to nag, but you should attach the file when you page me back. "}
	
EMAIL[EMAIL_PERSONAL][EMAIL_SIGNATURE] = {
	"Thanks a lot, man. It really means a lot to me.",
	"I hope you decide to do this for me. It would really mean a lot to me. Thanks for considering. ",
	"I would greatly appreciate if you decide to do this. If not, though, it's okay. I totally understand, mate. ",
	"As always, &PLAYER&, you're the bomb! Thanks a lot! ",
	"Keep doing what you do best, man! Peace out, &FNAME&. "
}

EMAIL[EMAIL_PERSONAL][EMAIL_MISSION_SABOTAGE_SINGLE] = {
	"I don't mean to sound whiny, but a friend of mine was recently canned by &TARGET_BUSINESS& for a bullshit reason. I was wondering if you can, uhh... put them back into line... by deleting a file for me? The file is named \"&TARGET_FILE&\" - don't ask how I know - and it's considered a pretty important file. I'm sure they'd be a bit distressed if something happened to it... What do you say? ",
	"A buddy of mine was outbid by some dummie while applying for a job at &TARGET_BUSINESS&. At the risk of sounding childish, I was wondering if you think you can show those airheads their place. A little birdie told me there's a file named \"&TARGET_FILE&\" in their database that plays a pretty big role. I bet they'll be regretting hiring that dummie as an IT over my buddy when they find it missing, heh heh. You up to it? ",
	"A friend of mine dug up some pretty nasty dirt about &TARGET_BUSINESS&. They've been trying to keep it under the rug, but they slipped somewhere. If what they're doing isn't a crime, then it's certainly criminal. I'm not one to play vigilante, but something needs to be done to those sickos. I was wondering if maybe you could help? We've been digging around, and it seems like the file \"&TARGET_FILE&\" is pretty important. Deleting that should slow them down - hopefully enough for this information to get released to the public and have them shut down for good. You think you could do it for me? "
}


-- Corporate
EMAIL[EMAIL_CORPORATE] = {}

EMAIL[EMAIL_CORPORATE][EMAIL_BREAK] = {"\n"}
EMAIL[EMAIL_CORPORATE][EMAIL_GREETING] = {
	"Hello, Mr. &P_LNAME&. ",
	"Greetings, Mr. &P_LNAME&. ",
	"Good day, Mr. &P_LNAME&. ",
	"Salutations, Mr. &P_LNAME&. "
}

EMAIL[EMAIL_CORPORATE][EMAIL_BUSINESS] = {
	"I am a representive for &BUSINESS&, and on behalf of my corporation, I am extending an offer to you. ",
	"I represent &BUSINESS&, and would like to present an offer to you. ",
	"I am an employee of &BUSINESS&, and there is an offer we would like you to consider. ",
	"&BUSINESS& has an offer that they would like you to consider. ",
	"My name is &FNAME& &LNAME&, and on behalf of the corporation I work for, &BUSINESS&, I would like to present to you an offer. "
}

EMAIL[EMAIL_CORPORATE][EMAIL_PAYMENT] = {
	"I have been authorized to transfer &PAYMENT& credits to your account upon completion. ",
	"Payment will be &PAYMENT& credits, non-negotiable. Upon completion, the credits will be transferred. ",
	"We here at &BUSINESS& hope that &PAYMENT& credits will be sufficient payment for a job of this magnitude. ",
	"Upon completion, &PAYMENT& credits will be transferred to your account. "
}

EMAIL[EMAIL_CORPORATE][EMAIL_CAUTION] = {}

EMAIL[EMAIL_CORPORATE][EMAIL_ATTACHMENT] = {
	"The files necessary for this mission have been sent as an attachment to this message. "
}

EMAIL[EMAIL_CORPORATE][EMAIL_HOW_TO_CONTACT] = {
	"When the job is finished, please contact me at &EMAIL&. ",
	"You can contact me at &EMAIL& once the job has been completed. ",
	"Once you are finished, please contact me at &EMAIL&. ",
	"My email is &EMAIL&. I will be waiting to hear from you that the job is completed. "
}

EMAIL[EMAIL_CORPORATE][EMAIL_INCLUDE_FILE] = {
	"Just so we are clear, you will need to include the file as an attachment when you are finished with the job. "
}

EMAIL[EMAIL_CORPORATE][EMAIL_SIGNATURE] = {
	"Best regards, &FNAME& &LNAME& of &BUSINESS&. ",
	"I wish you luck, Mr. &P_LNAME&. ",
	"Thank you for your time, Mr. &P_LNAME&. ",
	"On behalf of &BUSINESS&, I would like to personally thank you for your time. "
}

EMAIL[EMAIL_CORPORATE][EMAIL_MISSION_SABOTAGE_SINGLE] = {
	"&TARGET_BUSINESS& has been causing us a string of problems here at &BUSINESS&, and my supervisors have been getting tired of their shenanigans. They have decided it is about time something is done about them, to put them in their place. The offer I am making you now is for just that: we have had an insider leak information that &TARGET_BUSINESS& has grown fairly reliant on their computer systems, and that the file \"&TARGET_FILE&\" is particularly important for their operation. We would like you to infiltrate their systems and delete it. ",
	"We have recently received a tip that &TARGET_BUSINESS& have been investing large sums of money into some proprietary research that could cause undue stress on our market. We have enough competition as it is, and my supervisors fear that this research may push us out and put &TARGET_BUSINESS& up to being top dog in the industry. We can not allow that to happen. We've learned from a private source that the file \"&TARGET_FILE&\" is critical to this research, and without that file, their research will be slowed significantly, if not halted altogether. We want you to make that file disappear. ",
	"A former &TARGET_BUSINESS& employee recently informed my supervisors that &TARGET_BUSINESS& secretly hired a third party to infiltrate our databases and steal some incredibly sensitive information, with intentions of using that information to either blackmail us into exiting the industry, or using it to outbid ourselves with our own game. It is imperative that they do not do anything with that information. We want you to get into their systems and delete it before they have a chance to learn anything critical from it. The file's name is \"&TARGET_FILE&\". "
}

-- Serverside functions
if SERVER then
	function GenerateEmail(user,index)
		local mission = MissionList[index]
		
		local ptype,mtype = mission.Personality,mission.MissionType
		
		local vars = ""
		for var,value in pairs(mission.Variables) do
			vars=vars..var.."="..value.."\n"
		end
		
		umsg.Start("Generate_Email",user)
		umsg.Short(ptype)
		umsg.Short(mtype)
		umsg.String(vars)
		umsg.End()
	end
end

-- Clientside functions
if CLIENT then
	usermessage.Hook("Generate_Email",function(um)
		local personality,mission_type,sVars,bCaution = um:ReadShort(),um:ReadShort(),um:ReadString(),um:ReadBool()
		
		local structure = MISSION[mission_type].Structure
		
		-- First, build the string from the mission structure.
		local str = ""
		for k,enum in pairs(structure) do
			if (enum==EMAIL_CAUTION and bCaution) or (enum~=EMAIL_CAUTION) then -- Not all missions have cautions. If this mission doesn't, don't include the caution string.
				str = str..(table.Random(EMAIL[personality][enum]) or "ERROR! INVALID ENUMERATION!")
			end
		end
		
		-- Then, replace the variables as they appear
		local vars = {}
		for k,line in pairs(string.Explode("\n",sVars)) do
			local plode = string.Explode("=",line)
			if string.len(plode[1]) > 2 then
				vars[plode[1]] = plode[2]
			end
		end
		
		for var,value in pairs(vars) do
			str = string.gsub(str,"&"..string.upper(var).."&",value)
		end
		
		-- And finally store the string. This will probably be changed depending on how the email system is decided to be done.
		print(str)
	end)
end
